package goodenglish.domain.service.media;

import goodenglish.domain.model.lexicon.NewWord;
import goodenglish.domain.model.subt.Subt;
import goodenglish.domain.model.subt.SubtSentence;
import goodenglish.interfaces.videoplay.SentenceForPlay;

import java.util.HashSet;
import java.util.Set;

public class PlaySubt {
	private Subt subt;

	public PlaySubt(Subt subt) {
		this.subt = subt;
	}

	/**
	 * 得到单词学习
	 * 
	 * @param index
	 *            开始的句子 (从0开始)
	 * @param num
	 *            句子个数
	 * @return 句子显示
	 */
	public SentenceForPlay getSubtSentencesForPlay(int sindex, int num) {
		SentenceForPlay sentenceForPlay = new SentenceForPlay();
		StringBuffer enBuffer = new StringBuffer();
		StringBuffer chBuffer = new StringBuffer();
		StringBuffer explainBuffer = new StringBuffer();
		Set<String> spellSet = new HashSet<String>();
		for (int i = 0; i < num; i++) {
			// 添加英语字幕
			SubtSentence sentence = subt.getSubtSentences().get(sindex + i);
			enBuffer.append(sentence.getEnStr());
			enBuffer.append("\r\n");
			// 得到句子中的生单词
			// 原方案是先从句子中先得到生词，如果有空格就不行了
			// 改用新方案，遍历生词，
			for (NewWord newWord : subt.getLexicon().getNewWords()) {
				if (sentence.containsWord(newWord.getSpell())) {
					// 防止有重复的意思
					if (null != newWord
							&& !spellSet.contains(newWord.getSpell())) {
						spellSet.add(newWord.getSpell());
						explainBuffer.append(newWord.getSpell());
						explainBuffer.append(":");
						explainBuffer.append(newWord.getShortExplain());
						explainBuffer.append("   ");
						sentenceForPlay.subtWordCounts +=1;
					}
				}
			}

			chBuffer.append(sentence.getChStr());
			chBuffer.append("   ");

		}
		sentenceForPlay.enStrs = enBuffer.toString();
		sentenceForPlay.chStrs = chBuffer.toString();
		sentenceForPlay.explainStrs = explainBuffer.toString();
		enBuffer.append("\r\n").append(chBuffer).append("\r\n\r\n")
				.append(explainBuffer);
		
		sentenceForPlay.lastEtime = subt.getSubtSentences()
				.get(sindex + num - 1).getEtime();
		return sentenceForPlay;
	}
}
